package com.gxa.employment.controller;

import com.gxa.employment.common.Result;
import com.gxa.employment.entity.dto.LoginDto;
import com.gxa.employment.entity.dto.PageWithPhoneDto;
import com.gxa.employment.entity.dto.UpdatePwdDto;
import com.gxa.employment.entity.pojo.Manager;
import com.gxa.employment.entity.vo.PageVo;
import com.gxa.employment.service.ManagerService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * <p>
 * 管理员 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/mgr")
@Tag(name = "工作人员管理", description = "工作人员相关操作API")
public class ManagerController {
    @Autowired
    private ManagerService managerService;

    /**
     * 登录方法
     *
     * @return
     */
    @PostMapping("/login")
    @Operation(summary = "登录")
    public Result<HashMap<String, Object>> login(@RequestBody @Validated LoginDto loginDto) throws Exception {
        //封装返回的数据
        return Result.success(managerService.login(loginDto));
    }

    /**
     * 发送验证码接口
     * 该接口用于向指定的手机号码发送验证码
     *
     * @param phone 用户的手机号码，用于接收验证码
     * @return 返回操作结果，成功则返回成功信息
     * @throws Exception 如果发送验证码失败，则抛出Exception异常
     */
    @GetMapping("/sms")
    @Operation(summary = "发送验证码")
    public Result sms(String phone) throws Exception {
        managerService.sms(phone);
        return Result.success();
    }

    /**
     * 注册操作
     * @return
     */
    @PostMapping("/register")
    @Operation(summary = "注册")
    public Result<Manager> register(@RequestBody @Validated LoginDto loginDto) throws Exception {
        //注册操作
        managerService.register(loginDto);
        //封装返回的数据
        return Result.success();
    }

    /**
     * 新增管理员
     *
     * @return
     */
    @PostMapping("/add")
    @Operation(summary = "添加管理员")
    public Result<Manager> add(@RequestBody Manager manager){
        //调用自己的add方法，分配角色
        managerService.add(manager);
        return Result.success();
    }

    /**
     * 修改数据
     *
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    @Operation(summary = "修改管理员")
    public Result<Manager> update(@RequestBody Manager manager){
        managerService.updateById(manager);
        return Result.success();
    }

    /**
     * 删除管理人员信息接口
     * 根据传入的ID删除对应的管理人员信息，并返回操作结果
     *
     * @param id 要删除的管理人员ID
     * @return 返回操作结果，成功则返回成功信息
     */
    @DeleteMapping("/delete")
    @Operation(summary = "删除管理员")
    public Result<Manager> delete(@RequestParam("id") Integer id){
        managerService.removeById(id);
        return Result.success();
    }

    @PostMapping("/getByPage")
    @Operation(summary = "分页模糊查询")
    public Result<PageVo<Manager>> getByPage(@RequestBody PageWithPhoneDto dto) throws Exception {
        PageVo<Manager> list = managerService.getByPage(dto);
        return Result.success(list);
    }

    /**
     * 根据手机号查询
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getByPhone")
    @Operation(summary = "手机号查询用户详情")
    public Result<Manager> getByPhone(@RequestParam("phone") String phone) throws Exception {
        return Result.success(managerService.getByPhone(phone));
    }

    /**
     * 根据id查询管理员数据
     *
     * @return
     * @throws Exception
     */
    @GetMapping("/getById")
    @Operation(summary = "通过id查询管理员详情")
    public Result<Manager> getById(@RequestParam("id") Integer id) throws Exception {
        Manager manager = managerService.getById(id);
        return Result.success(manager);
    }

    @PostMapping("/updatePwd")
    @Operation(summary = "修改密码")
    public Result updatePwd(@RequestBody UpdatePwdDto dto) throws Exception {
        managerService.updatePwd(dto);
        return Result.success();
    }

}
